<?php
/**
 * Created by PhpStorm.
 * User: mhx

 */


class artistcomehereController extends baseController{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     *
     */
    public function run(){
        $url_perfix = 'http://artist.artron.net/class-0-0-';
        $url_postfix = '.html';

        for($i=1;$i<358;$i++){
            echo '画家',$i;
            $url = $url_perfix.$i.$url_postfix;
            $contents = url_crawler($url);
            if($contents !=false){
                $urllist = $this->getUrllist($contents);
//                print_r($urllist);die;
                $j = 0;
                while (isset($urllist[$j])){
                    $waittosave = array();
//                    print_r($urllist);die;
                   print_r($urllist[$j]);
//                    echo '<------>';
//                    echo $j;
                    //只抓取其中about页面的数据
                    $relurl = $urllist[$j]['detailsurl'].'/about';
                    $contents = url_crawler($relurl);
                    $pos = mb_strpos($contents,'或许，点击这里');
                    if($pos !== false && $contents == ''){
                        ++$j;
                        continue;
                    }
                    $waittosave =$this->getdetails($contents,$urllist[$j]);
                    //echo 'tosave::::';
                    //print_r($waittosave);die;
                    if($waittosave == false){
                        ++$j;
                        continue;
                    }
//                    echo '2312';
                    $ishere = model('maartist')->select('name="'.$waittosave['name'].'"');
//                    print_r($ishere);die;
                    $yicunzai = 0;
                    for($here=0;isset($ishere[$here]);$here++){
//                        print_r($ishere);
                        if($ishere[$here]['detailsurl'] == $waittosave['detailsurl']){
                            $yicunzai =1;
                        }else{
                            continue;
                        }
                    }
//                    echo 1;
                    if($yicunzai ==1){++$j; continue;}

                    if($waittosave['imgurl']!=''){
                        $image = $this->saveImage($waittosave['imgurl']);
                        if($image != false){
                            $waittosave['imgpath'] = 'artist/'.$image;
                        }else{$waittosave['imgpath'] = '';}
                    }else{$waittosave['imgpath'] = '';}

                    $waittosave['year'] = $this->deleteunit($waittosave['year'],'\n年表\n中文English');
                    $waittosave['style'] = $this->deleteunit($waittosave['style'],'\n艺术风格');
                    $waittosave['award'] = $this->deleteunit($waittosave['award'],'\n获奖中文English');
                    $waittosave['runge'] = $this->deleteunit($waittosave['runge'],'\n润格');
                    $waittosave['contact'] = $this->deleteunit($waittosave['contact'],'\n中文English');
                    //print_r($waittosave);
                    /*if($waittosave['cityid']==''||$waittosave['cityid']==null||$waittosave['cityid']==false){
                        $waittosave['cityid']=0;
                    }*/
                    $id = model('maartist')->insert($waittosave);

                    //存数据表里name,id;
                    $fix = model('mazhanxunfix')->select('id=1');
                    $fix[0]['artist'] = $fix['artist'].$waittosave['name'].','.$id.';';
                    //print_r($fix[0]);die;
                    model('mazhanxunfix')->update('id=1',$fix[0]);
                    //die;

//                    echo 'daol!!!!!!!!!!!!!!';
                    ++$j;
                }

            }
        }
        return null;
    }

    //通过列表页得到所有的详情页url，籍贯地区，所属类别，毕业院校
    protected function getUrllist($contents){
        $pos = mb_strpos($contents,'class="shopList">');
        $urllist = array();
        $i = 0 ;
        while ($pos !== false){
            $contents = mb_substr($contents,$pos+mb_strlen('class="shopList">'));
            $pos = mb_strpos($contents,'</a');
            $urllist[$i]['name'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));

            model('maartist')->find('name="'.$urllist[$i]['name'].'"');

            $pos = mb_strpos($contents,'class="pic_set">');
            $contents = mb_substr($contents,$pos +mb_strlen('class="pic_set">'));
            $pos = mb_strpos($contents,'<a href="');
            $contents = mb_substr($contents,$pos +mb_strlen('<a href="'));
            $pos = mb_strpos($contents,'"');
            $urllist[$i]['detailsurl'] = $this->killkongge(mb_substr($contents,0,$pos));
            $pos = mb_strpos($contents,'<img src="');
            $contents = mb_substr($contents,$pos+mb_strlen('<img src="'));
            $pos = mb_strpos($contents,'"');
            $urllist[$i]['imgurl'] = mb_substr($contents,0,$pos);
            $pos = mb_strpos($contents,'籍贯地区：');
            $contents = mb_substr($contents,$pos+mb_strlen('籍贯地区：'));
            $pos = mb_strpos($contents,'</b');
            $urllist[$i]['hometown'] = $this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
            $pos = mb_strpos($contents,'所属类别：');
            $contents = mb_substr($contents,$pos+mb_strlen('所属类别：'));
            $pos = mb_strpos($contents,'</b');
            $urllist[$i]['thetype'] = $this->killkongge(strip_tags(mb_substr($contents,0,$pos)));
            $pos = mb_strpos($contents,'毕业院校：');
            $contents = mb_substr($contents,$pos+mb_strlen('毕业院校：'));
            $pos = mb_strpos($contents,'</b');
            $urllist[$i]['university'] =$this->killkongge(strip_tags(mb_substr($contents,0,$pos)));

            $pos = mb_strpos($contents,'class="shopList">');
            ++$i;
        }

        return $urllist;
    }

    //抓取详情页的about的数据
    //返回的数据可以直接存储
    protected function getdetails($contents,$olddata){
        /*
         * introduction简介 'class="introTxt">'    <--->    id="encontent" style="display:none;">
         *  contact联系方式    name="contacT">   <---->  id="enDetail" style="display:none;">
         *  year年表      name="year">   <----->    id="enintroAward" style="display:none;">
         *  style艺术风格    name="style">  <----->  </div
         *  award获奖      name="award">  <----->      style="display:none" id="enhjDetail">
         * runge润格       name="renge"> <-----> </p
         * xglj相关连接 name="xglj">  <------->  class="clear">
         *
         *
         */

        $ret = array();
        //连简介都没有，不值得我去存
        $pos = mb_strpos($contents,'class="introTxt">');
        //print_r($olddata);die;
        if($pos == false){
            return $this->getdetails2($contents,$olddata);
        }

        $contents = mb_substr($contents,$pos+mb_strlen('class="introTxt">'));
        $pos = mb_strpos($contents,'id="encontent" style="display:none;">');
        if($pos ==false){
            return false;
        }
        $introduction = strip_tags(mb_substr($contents,0,$pos+mb_strlen('id="encontent" style="display:none;">')));
        $ret['introduction'] = $this->keeponeline($introduction);


        $pos = mb_strpos($contents,'name="contacT">');
        if($pos !=false){
            $contents = mb_substr($contents,$pos+mb_strlen('name="contacT">'));
            $pos = mb_strpos($contents,'id="enDetail" style="display:none;">');
            if($pos !=false){
                $ret['contact'] = strip_tags(mb_substr($contents,0,$pos+mb_strlen('id="enDetail" style="display:none;">')));
//                echo 'cont qian:::',$ret['contact'],'<br />';
                $ret['contact'] =$this->keeponeline($ret['contact']);
//                echo 'hou:::',$ret['contact'],'<br />';
            }else{
                $ret['contact'] = '';
            }
        }else{
            $ret['contact'] = '';
        }

        $pos = mb_strpos($contents,'name="year">');
        if($pos !== false){
            $contents = mb_substr($contents,$pos + mb_strlen('name="year">'));
            $pos = mb_strpos($contents,'id="enintroAward" style="display:none;">');
            if($pos !== false){
                $ret['year'] = strip_tags(mb_substr($contents,0,$pos+mb_strlen('id="enintroAward" style="display:none;">')));
//                echo 'year qian:::',$ret['year'],'<br />';
                $ret['year'] =$this->keeponeline($ret['year']);
//                echo 'hou:::',$ret['year'],'<br />';die;

            }else{
                $ret['year'] = '';
            }
        }else{
            $ret['year'] = '';
        }

        $pos = mb_strpos($contents,'name="style">');
        if($pos !== false){
            $contents = mb_substr($contents,$pos +mb_strlen('name="style">'));
            $pos = mb_strpos($contents,'</div');
            if($pos !== false){
                $ret['style'] = strip_tags(mb_substr($contents,0,$pos));
                $ret['style'] =$this->keeponeline($ret['style']);
            }else{
                $ret['style'] = '';
            }
        }else{
            $ret['style'] = '';
        }

        $pos = mb_strpos($contents,'name="award">');
        if($pos !== false){
            $contents = mb_substr($contents,$pos +mb_strlen('name="award">'));
            $pos = mb_strpos($contents,'style="display:none" id="enhjDetail">');
            if($pos !== false){
                $ret['award'] = strip_tags(mb_substr($contents,0,$pos+mb_strlen('style="display:none" id="enhjDetail">')));
//                echo 'awa qian:::',$ret['award'],'<br />';
                $ret['award'] =$this->keeponeline($ret['award']);
//                echo 'hou:::',$ret['award'],'<br />';die;

            }else{
                $ret['award'] = '';
            }
        }else{
            $ret['award'] = '';
        }

        $pos = mb_strpos($contents,'name="renge">');
        if($pos !== false){
            $contents = mb_substr($contents,$pos + mb_strlen('name="renge">'));
            $pos = mb_strpos($contents,'</p');
            if($pos !== false){
                $ret['runge'] = strip_tags(mb_substr($contents,0,$pos));
                $ret['runge'] =$this->keeponeline($ret['runge']);
            }else{
                $ret['runge'] = '';
            }
        }else{
            $ret['runge'] = '';
        }

        /*$pos = mb_strpos($contents,'name="xglj">');
        if($pos != false){
            $contents = mb_substr($contents,$pos +mb_strlen('name="xglj">'));
            $pos = mb_strpos($contents,'class="clear">');
            if($pos !=false){
                $ret['xglj'] = strip_tags(mb_substr($contents,0,$pos+mb_strlen('class="clear">')));
            }
        }*/

        $ret['name'] = $olddata['name'];
        $ret['detailsurl'] = $olddata['detailsurl'];
        $ret['imgurl'] = $olddata['imgurl'];
        $ret['hometown'] = $olddata['hometown'];
        $ret['thetype'] = $olddata['thetype'];
        $ret['university'] = $olddata['university'];
        return $ret;
    }

    protected function getdetails2($contents,$olddata){

        $ret = array();
        $pos = mb_strpos($contents,'class="mainCon">');
        if($pos ==false){
            return false;
        }
        $contents = mb_substr($contents,$pos+mb_strlen('class="mainCon">'));
        $pos = mb_strpos($contents,'src="');
        if($pos !== false){
            $contents = mb_substr($contents,$pos +mb_strlen('src="'));
            $pos = mb_strpos($contents,'"');
            $ret['imgurl'] = mb_substr($contents,0,$pos);
        }
        $pos = mb_strpos($contents,'p>');
        $contents = mb_substr($contents,$pos+mb_strlen('p>'));
        $pos = mb_strpos($contents,'class="artistCon year">');
        $ret['introduction'] = $this->keeponeline(strip_tags(mb_substr($contents,0,$pos+mb_strlen('class="artistCon year">'))));

        $contents = mb_substr($contents,$pos+mb_strlen('class="artistCon year">'));
        $pos = mb_strpos($contents,'--相关链接-->');
        $ret['year'] =$this->keeponeline(strip_tags(mb_substr($contents,0,$pos+mb_strlen('--相关链接-->'))));

        $pos = mb_strpos($contents,'--获奖-->');
        $contents = mb_substr($contents,$pos +mb_strlen('--获奖-->'));

        $pos = mb_strpos($contents,'--艺术风格-->');
        $ret['award'] = $this->keeponeline(strip_tags(mb_substr($contents,0,$pos+mb_strlen('--艺术风格-->'))));

        $contents = mb_substr($contents,$pos +mb_strlen('--艺术风格-->'));
        $pos = mb_strpos($contents,'--润格-->');

        $ret['style'] =  $this->keeponeline(strip_tags(mb_substr($contents,0,$pos+mb_strlen('--润格-->'))));

        $ret['runge'] = '';
        $ret['contact'] = '';
        $ret['name'] = $olddata['name'];
        $ret['detailsurl'] = $olddata['detailsurl'];
        $ret['imgurl'] = $olddata['imgurl'];
        $ret['hometown'] = $olddata['hometown'];
        $ret['thetype'] = $olddata['thetype'];
        $ret['university'] = $olddata['university'];

//        print_r($ret);die;
        return $ret;

    }




    protected function saveImage($img_url){

        /*echo 'This is save Img:::';
        echo $img_url;
        echo '<br />';*/

        $folder = ROOT_PATH.'artist/';

        if(!is_dir($folder)){
            mkdir($folder,0777);
            //chmod($folder,0777);
        }

        //echo 'url：',$img_url,'<br />';

        $pic_format = array('.bmp','.jpg','.tiff','.gif','.pcx','.tga','.exif','.jpeg','.png',
            '.fpx','.svg','.psd','.cdr','.pcd','.dxf','.ufo','.eps','.ai','.raw','.WMF');

        //echo 'saveImage:','<br />','imgurl:',$img_url,'<br />','path:',$folder,'<br />';
        $img_url = str_replace('\\','',$img_url);

        $end = explode(".", $img_url);
        $temp_i = 0;
        while(isset($end[$temp_i])){
            ++$temp_i;
        }
        $end = "." . $end[$temp_i-1];

        if(!in_array($end,$pic_format)){
            return false;
        }
        //echo 'url没问题','<br />';
        $ch = curl_init();
        //echo 'curl没问题','<br />';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        //要头信息
        curl_setopt($ch,CURLOPT_HEADER,1);
        //不要正文
        curl_setopt($ch, CURLOPT_NOBODY, 1);

        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $contents = curl_exec($ch);
        //echo 'contents::',$contents,'<br />';

        /*$content_type = curl_getinfo($ch,CURLINFO_CONTENT_TYPE);

        echo $content_type;die;*/
        curl_close($ch);

        //echo '$contents:::',$contents,'<br />';
        $pos = mb_stripos($contents,'200');
        if($pos === false){
            $pos = mb_strpos($contents,'OK');
            if($pos === false){
                return '';
            }
        }

        $pic_name = $this->utime() . rand(1111, 9999) . $end;

        //set_time_limit(8 * 60 * 60); //限制最大的执行时间
        $destination_folder = $folder; //文件下载保存目录
        $newfname = $destination_folder . $pic_name;//文件PATH

        //echo 'newfname:',$newfname,'<br />';

        $ch = curl_init();
        try{$fp = fopen($newfname,'wb');}catch(Exception $err){
            cpError::write($err->getMessage());
            return 0;}
        //echo 'fopen is ok!!';
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch,CURLOPT_URL,$img_url);
        curl_setopt($ch,CURLOPT_FILE,$fp);

        curl_exec($ch);
        curl_close($ch);
        fclose($fp);

        //生成缩略图
        Image::thumb($newfname,$destination_folder.'64_'.$pic_name,'','640','640',6);


        /*$image = new \Think\Image();
        //echo $newfname;
        try{$image->open($newfname);}catch (Exception $err){return 0;}
        echo 'Thinkphp open is right!!!';
        // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
        //echo $destination_folder.'64_'.$pic_name;die;
        try{$image->thumb(640, 640)->save($destination_folder.'64_'.$pic_name);}catch (Exception $err){return 0;}
        //echo 'Thinkphp thumb is right!!','<br />';
        //die;*/

        return $pic_name;
    }

    protected function utime()
    {
        return time();
    }

    protected function killkongge($str){
        $search = array(" ","　","\n","\r","\t");
        $replace = array("","","","","");
        return str_replace($search, $replace, $str);
    }
    protected function keeponeline($str){
        //先删空格再把多个换行符替换成一个
        $str = preg_replace ( "/\s(?=\s)/","\\1", $str);

        $search = array(" ");
        $replace = array("");
        $str =  str_replace($search, $replace, $str);

        $search = array("\n","\r","\t");
        $replace = array(" "," "," ");
        $str =  str_replace($search, $replace, $str);

        $str = preg_replace('/\s+/','\n',$str);

        $str = str_replace('&nbsp;','',$str);

        return $str;
    }

    /*
         * 字符串删除第一个或最后一个指定字符串，目前用于删除不用的单位
         *$theend = null ,删除第一个匹配，否则删除最后一个匹配
         * $p要删除的字符串
         */
    protected function deleteunit($str,$unit,$theend = null){
        if($theend != null){
            $relpos =0;
            //$lastpos = 0;
            $pos = mb_strpos($str,$unit);
            $temp = $str;
            //echo $str,'<br />';
            //echo 'pos:::',$pos,'<br />';
            if($pos  === false){return $str;}
            while($pos !== false){
//                echo 'while::relpos::',$relpos,'<br />';
//                echo 'while::pos::',$pos,'<br />';

                $relpos = $relpos+$pos+mb_strlen($unit);
                //$lastpos = $pos;
                $temp = mb_substr($temp,$pos+mb_strlen($unit));

//                echo 'temp:::',$temp,'<br />';

                $pos = mb_strpos($temp,$unit);

            }
//            echo 'out::relpos::',$relpos,'<br />';
//            echo 'out::lastpos::',$lastpos,'<br />';
            $relpos -= mb_strlen($unit);
            /*if($relpos != $lastpos){
                $relpos = $relpos + $lastpos;
            }*/

            //echo 'out::relpos::',$relpos,'<br />';
            //echo 'length::unit::',$unit,'<br />';
            $qian = mb_substr($str,0,$relpos);
            //echo 'qian::',$qian,'<br />';
            $hou = mb_substr($str,$relpos+mb_strlen($unit));
            //echo 'hou::',$hou,'<br />';
            $ret = $qian.$hou;

            return $ret;

        }else{
            $pos = mb_strpos($str,$unit);
            if($pos  === false){return $str;}
            $qian = mb_substr($str,0,$pos);
            $hou = mb_substr($str,$pos+mb_strlen($unit));
            $ret = $qian.$hou;
            return $ret;
        }
    }




}